// dragwinView.h : interface of the CDragwinView class
//
/////////////////////////////////////////////////////////////////////////////

#pragma once
#include <map>
#include <memory>
#include "dragablewin.h"


class CDragwinView : public CWindowImpl<CDragwinView>
{
protected:
	std::map<HWND, std::shared_ptr<CDragableWin>> dragableWins;
	HWND hwndActivate = NULL;
public:
	DECLARE_WND_CLASS(NULL)

	BOOL PreTranslateMessage(MSG* pMsg)
	{
		pMsg;
		return FALSE;
	}

	BEGIN_MSG_MAP(CDragwinView)
		MESSAGE_HANDLER(WM_PAINT, OnPaint)
		MESSAGE_HANDLER(WM_LBUTTONDOWN, OnMouseClick)
		MESSAGE_HANDLER(EM_ACTIVE, OnSubRectActive)
	END_MSG_MAP()

// Handler prototypes (uncomment arguments if needed):
//	LRESULT MessageHandler(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
//	LRESULT CommandHandler(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
//	LRESULT NotifyHandler(int /*idCtrl*/, LPNMHDR /*pnmh*/, BOOL& /*bHandled*/)

	LRESULT OnMouseClick(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
	{
		if (hwndActivate != NULL) {
			auto p = dragableWins.find(hwndActivate);
			if (p != dragableWins.end()) {
				p->second->Deactivate();
			}
		}
		return 0;
	}

	LRESULT OnPaint(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
	{
		CPaintDC dc(m_hWnd);

		//TODO: Add your drawing code here

		return 0;
	}

	LRESULT  OnSubRectActive(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& /*bHandled*/) 
	{
		hwndActivate = (HWND)wParam;
		return 0;
	}

	void CreateRect(RECT &rc) {
		std::shared_ptr<CDragableWin> p = std::make_shared<CDragableWin>();
		p->Create(m_hWnd, rc);
		p->ShowWindow(SW_SHOW);
		dragableWins[p->m_hWnd]=p;
	}
};
